package com.lb.activity.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lb.activity.mapper.PicManagementMapper;
import com.lb.activity.po.PicManagement;
import com.lb.activity.sys.service.PicManagementService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.util.SnowflakeIdWorker;
import com.lb.activity.util.emumdata.DataEnum;
import com.lb.activity.vo.PicManagementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * pic管理
 * @author 001030
 */
@Service
public class PicManagementServiceImpl implements PicManagementService {

    @Autowired
    private SnowflakeIdWorker idWorker;

    @Resource
    private PicManagementMapper picManagementMapper;

    @Override
    public ResultUtil addPicManagement(PicManagementVo picManagementVo) {
        PicManagement picManagement = PicManagement.builder().build();
        BeanUtils.copyProperties(picManagementVo,picManagement);
        picManagement.setId(idWorker.nextId());
        Integer insert = picManagementMapper.insertSelective(picManagement);
        return ResultUtil.ok(insert>0);
    }

    @Override
    public ResultUtil pics(PicManagementVo picManagementVo) {
        PageHelper.startPage(picManagementVo.getPage(),picManagementVo.getLimit());
        Example example = new Example(PicManagement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",DataEnum.INSERT_STATUS.getStatus());
        List<PicManagement> picManagements = picManagementMapper.selectByExample(example);
        PageInfo<PicManagement> pageInfo = new PageInfo(picManagements);
        return ResultUtil.build(0,"",Integer.parseInt(pageInfo.getTotal()+""),picManagements);
    }

    @Override
    public ResultUtil removePic(Long id) {
        PicManagement picManagement = PicManagement.builder().id(id).status(DataEnum.DELETE_STATUS.getStatus()).build();
        int i = picManagementMapper.updateByPrimaryKeySelective(picManagement);
        return ResultUtil.ok(i>0);
    }
}
